package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Entity.PeriodicReport;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.JComboBox;


//import org.jfree.chart.ChartFactory;
//import org.jfree.chart.ChartFrame;
//import org.jfree.chart.JFreeChart;
//import org.jfree.chart.plot.CategoryPlot;
//import org.jfree.chart.plot.PlotOrientation;
//import org.jfree.data.category.DefaultCategoryDataset;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 * this is the GUI for view periodic reports.
 * @author mabos555
 *
 */
public class ViewPeriodicReport extends JFrame {

	private JPanel contentPane;
	private JTextField TotalComp;
	private JPanel panel;
	private JLabel lblTotal;
	private JLabel lblMedian;
	private JTextField medianComp;
	private JButton btnFrequencyDistribuition;
	private JPanel panel_1;
	private JLabel lblDateDate;
	private JLabel label;
	private JTextField TotalCancel;
	private JLabel label_1;
	private JTextField medianCancel;
	private JButton button;
	private JPanel panel_2;
	private JTextField totalDisable;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField medianDisable;
	private JButton button_1;
	private JLabel lblWeek;
	private JComboBox<Integer> comboBox;
	private JButton btnReturn;
	private Window frame;

	/**
	 *  is the constructor. it has a table to chose from and save after validate.
	 */
	public ViewPeriodicReport(PeriodicReport p) 
	{
		final PeriodicReport p2=p;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 337);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPeriodicReport = new JLabel("Periodic Report");
		lblPeriodicReport.setBounds(131, 11, 94, 14);
		contentPane.add(lblPeriodicReport);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Complete Orders:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(0, 36, 434, 49);
		contentPane.add(panel);
		panel.setLayout(null);
		
		TotalComp = new JTextField();
		TotalComp.setBounds(53, 18, 39, 20);
		panel.add(TotalComp);
		TotalComp.setColumns(10);
		this.TotalComp.setText(String.valueOf(p.getNumOfComplete(1)+p.getNumOfComplete(2)+p.getNumOfComplete(3)+p.getNumOfComplete(4)+p.getNumOfComplete(5)+p.getNumOfComplete(6)));
		this.TotalComp.setEditable(false);
		
		lblTotal = new JLabel("Total:");
		lblTotal.setBounds(10, 21, 46, 14);
		panel.add(lblTotal);
		
		lblMedian = new JLabel("Median:");
		lblMedian.setBounds(125, 21, 46, 14);
		panel.add(lblMedian);
		
		medianComp = new JTextField();
		medianComp.setBounds(174, 18, 30, 20);
		panel.add(medianComp);
		medianComp.setColumns(10);
		this.medianComp.setText(String.valueOf(p.getMedianComplete()));
		medianComp.setEditable(false);
		
		btnFrequencyDistribuition = new JButton("Frequency Distribution");
		btnFrequencyDistribuition.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {/*
				DefaultCategoryDataset dataset=new DefaultCategoryDataset();
				int[] freq=p2.getCompleteFrequencyDist();
				
				for(int i=0; i<freq.length; i++)
					dataset.setValue(freq[i], "Number Of Parking Lot", String.valueOf(i+1));
				
				JFreeChart chart=ChartFactory.createBarChart("Frequency Distribution of Number of Complete Orders","Number Of Orders", "Number Of Parking Lot", dataset,PlotOrientation.VERTICAL,false,true,false);
				CategoryPlot p=chart.getCategoryPlot();
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame=new ChartFrame("Frequency Distribution",chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
			*/}
		});
		btnFrequencyDistribuition.setBounds(257, 17, 167, 23);
		panel.add(btnFrequencyDistribuition);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Canceled Orders:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(0, 108, 434, 49);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		label = new JLabel("Total:");
		label.setBounds(10, 19, 46, 14);
		panel_1.add(label);
		
		TotalCancel = new JTextField();
		TotalCancel.setColumns(10);
		TotalCancel.setBounds(53, 16, 39, 20);
		panel_1.add(TotalCancel);
		this.TotalCancel.setText(String.valueOf(p.getNumOfCanceled(1)+p.getNumOfCanceled(2)+p.getNumOfCanceled(3)+p.getNumOfCanceled(4)+p.getNumOfCanceled(5)+p.getNumOfCanceled(6)));
		TotalCancel.setEditable(false);
		
		label_1 = new JLabel("Median:");
		label_1.setBounds(125, 19, 46, 14);
		panel_1.add(label_1);
		
		medianCancel = new JTextField();
		medianCancel.setColumns(10);
		medianCancel.setBounds(174, 16, 30, 20);
		panel_1.add(medianCancel);
		this.medianCancel.setText(String.valueOf(p.getMedianCancel()));
		medianCancel.setEditable(false);
		
		button = new JButton("Frequency Distribution");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) 
			{/*
				DefaultCategoryDataset dataset=new DefaultCategoryDataset();
				int[] freq=p2.getCancelFrequencyDist();
				
				for(int i=0; i<freq.length; i++)
					dataset.setValue(freq[i], "Number Of Parking Lot", String.valueOf(i+1));
				
				JFreeChart chart=ChartFactory.createBarChart("Frequency Distribution of Number of Canceled Orders","Number Of Orders", "Number Of Parking Lot", dataset,PlotOrientation.VERTICAL,false,true,false);
				CategoryPlot p=chart.getCategoryPlot();
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame=new ChartFrame("Frequency Distribution",chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
			*/}
		});
		button.setBounds(257, 15, 167, 23);
		panel_1.add(button);
		
		lblDateDate = new JLabel(p.getStart().getDate()+"/"+(p.getStart().getMonth()+1)+"/"+(p.getStart().getYear()+1900)+" - "+p.getEndDate().getDate()+"/"+(p.getEndDate().getMonth()+1)+"/"+(p.getEndDate().getYear()+1900));
		lblDateDate.setBounds(235, 11, 100, 14);
		contentPane.add(lblDateDate);
		
		panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(null, "Disabled Places:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(0, 168, 434, 83);
		contentPane.add(panel_2);
		
		totalDisable = new JTextField();
		totalDisable.setColumns(10);
		totalDisable.setBounds(54, 52, 39, 20);
		panel_2.add(totalDisable);
		totalDisable.setEditable(false);
		
		label_2 = new JLabel("Total:");
		label_2.setBounds(10, 55, 46, 14);
		panel_2.add(label_2);
		
		label_3 = new JLabel("Median:");
		label_3.setBounds(125, 55, 46, 14);
		panel_2.add(label_3);
		
		medianDisable = new JTextField();
		medianDisable.setColumns(10);
		medianDisable.setBounds(174, 52, 30, 20);
		panel_2.add(medianDisable);
		medianDisable.setEditable(false);
		
		comboBox = new JComboBox<Integer>();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if((int)comboBox.getSelectedItem()==1)
				{
					totalDisable.setText(String.valueOf(p2.getWeeklyDisabled(1)[0]+p2.getWeeklyDisabled(2)[0]+p2.getWeeklyDisabled(3)[0]+p2.getWeeklyDisabled(4)[0]+p2.getWeeklyDisabled(5)[0]+p2.getWeeklyDisabled(6)[0]));
					medianDisable.setText(String.valueOf(p2.getDisabledWeeklyMed(0)));
				}
				else if((int)comboBox.getSelectedItem()==2)
				{
					totalDisable.setText(String.valueOf(p2.getWeeklyDisabled(1)[1]+p2.getWeeklyDisabled(2)[1]+p2.getWeeklyDisabled(3)[1]+p2.getWeeklyDisabled(4)[1]+p2.getWeeklyDisabled(5)[1]+p2.getWeeklyDisabled(6)[1]));
					medianDisable.setText(String.valueOf(p2.getDisabledWeeklyMed(1)));
				}
				else if((int)comboBox.getSelectedItem()==3)
				{
					totalDisable.setText(String.valueOf(p2.getWeeklyDisabled(1)[2]+p2.getWeeklyDisabled(2)[2]+p2.getWeeklyDisabled(3)[2]+p2.getWeeklyDisabled(4)[2]+p2.getWeeklyDisabled(5)[2]+p2.getWeeklyDisabled(6)[2]));
					medianDisable.setText(String.valueOf(p2.getDisabledWeeklyMed(2)));
				}
				else if((int)comboBox.getSelectedItem()==4)
				{
					totalDisable.setText(String.valueOf(p2.getWeeklyDisabled(1)[3]+p2.getWeeklyDisabled(2)[3]+p2.getWeeklyDisabled(3)[3]+p2.getWeeklyDisabled(4)[3]+p2.getWeeklyDisabled(5)[3]+p2.getWeeklyDisabled(6)[3]));
					medianDisable.setText(String.valueOf(p2.getDisabledWeeklyMed(3)));
				}
			}
		});
		comboBox.setBounds(54, 24, 46, 20);
		panel_2.add(comboBox);
		this.comboBox.addItem(1);
		this.comboBox.addItem(2);
		this.comboBox.addItem(3);
		this.comboBox.addItem(4);
		
		button_1 = new JButton("Frequency Distribution");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) 
			{/*
				DefaultCategoryDataset dataset=new DefaultCategoryDataset();
				int[] freq=p2.getWeeklyFrequencyDist((int)comboBox.getSelectedItem());
				
				for(int i=0; i<freq.length; i++)
					dataset.setValue(freq[i], "Number Of Parking Lot", String.valueOf(i+1));
				
				JFreeChart chart=ChartFactory.createBarChart("Frequency Distribution of Number of Disabled Parking Spots - Week "+String.valueOf((int)comboBox.getSelectedItem()),"Number Of Orders", "Number Of Parking Lot", dataset,PlotOrientation.VERTICAL,false,true,false);
				CategoryPlot p=chart.getCategoryPlot();
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame=new ChartFrame("Frequency Distribution",chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
			*/}
		});
		button_1.setBounds(257, 49, 167, 23);
		panel_2.add(button_1);
		
		lblWeek = new JLabel("Week:");
		lblWeek.setBounds(10, 27, 46, 14);
		panel_2.add(lblWeek);
		
		btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		btnReturn.setBounds(172, 262, 89, 23);
		contentPane.add(btnReturn);
	}
    public void close() {
        frame.setVisible(false);
        frame.dispose();
    }
}